package ljye.com.ljye.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import ljye.com.ljye.R;
import ljye.com.ljye.activity.Activity_Rating_Interpretation;
import ljye.com.ljye.dialog.Note_Dialog;
import ljye.com.ljye.utils.CommonViewHolder;


/**
 * 主页 简历
 */
public class Fragment_Teacher_home_resume extends Fragment {

    private View view;
    private LinearLayout identityNote;//认证说明
    private LinearLayout ratingInterpretation;//评级解读
    private ListView listview;
    private ArrayList<String> listData;
    private MyAdpapter mLvAdapter;
    private Intent intent;
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.identity_note://认证说明
                    dialog = new Note_Dialog(getActivity());
                    dialog.setYesOnclickListener("取消", new Note_Dialog.onYesOnclickListener() {
                        @Override
                        public void onYesClick() {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();
                    break;
                case R.id.rating_interpretation://认证说明
                    intent = new Intent(getActivity(), Activity_Rating_Interpretation.class);
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    };
    private Note_Dialog dialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_teacher_home_resume, container, false);
            listview = (ListView)view.findViewById(R.id.listview);
            identityNote = (LinearLayout)view.findViewById(R.id.identity_note);
            ratingInterpretation = (LinearLayout)view.findViewById(R.id.rating_interpretation);

            listData = new ArrayList<>();
            for (int i = 1; i <= 2; i++) {
                listData.add("创意思维" + i);
            }

            mLvAdapter = new MyAdpapter();
            listview.setAdapter(mLvAdapter);
            setListViewHeight(listview);

            identityNote.setOnClickListener(listener);
            ratingInterpretation.setOnClickListener(listener);
        }
        return view;
    }

    public class MyAdpapter extends BaseAdapter {

        @Override
        public int getCount() {
            return listData == null ? 0 : listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            CommonViewHolder holder = CommonViewHolder.createCVH(view, viewGroup, R.layout.itme_resume);
            TextView title = holder.getTv(R.id.tv_title);//title
            TextView content = holder.getTv(R.id.tv_content);//内容
            ImageView imageView = holder.getIv(R.id.iv);//图片

            ImageView iv = holder.getIv(R.id.iv);
            String s = listData.get(i);
            title.setText(s);
            return holder.convertView;
        }
    }
    //解决ScrollView包裹listview后只显示一个条目问题
    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
